package com.monitor.ui.controller;

import com.monitor.control.TimeController;
import com.monitor.filter.TextFilter;
import com.monitor.security.RegistrationManager;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;

import java.net.URL;
import java.util.ResourceBundle;

/**
 * 首页面板控制器，处理首页面板的交互逻辑和状态显示
 */
public class HomePanelController implements Initializable {

    @FXML
    private Label systemStatusLabel;

    @FXML
    private Label protectionStatusLabel;

    @FXML
    private Label controlStatusLabel;

    @FXML
    private Label registrationLabel;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        // 初始化状态信息
        updateSystemStatus();
        updateProtectionStatus();
        updateControlStatus();
        updateRegistrationStatus();
    }

    /**
     * 更新系统状态信息
     */
    private void updateSystemStatus() {
        try {
            systemStatusLabel.setText("正常运行");
            systemStatusLabel.setStyle("-fx-text-fill: green;");
        } catch (Exception e) {
            systemStatusLabel.setText("异常: " + e.getMessage());
            systemStatusLabel.setStyle("-fx-text-fill: red;");
        }
    }

    /**
     * 更新防护状态信息
     */
    private void updateProtectionStatus() {
        try {
            // 检查文本过滤器状态作为示例
            TextFilter textFilter = new TextFilter();
            boolean enabled = textFilter.isEnabled();
            
            if (enabled) {
                protectionStatusLabel.setText("已开启");
                protectionStatusLabel.setStyle("-fx-text-fill: green;");
            } else {
                protectionStatusLabel.setText("已关闭");
                protectionStatusLabel.setStyle("-fx-text-fill: gray;");
            }
        } catch (Exception e) {
            protectionStatusLabel.setText("异常: " + e.getMessage());
            protectionStatusLabel.setStyle("-fx-text-fill: red;");
        }
    }

    /**
     * 更新控制状态信息
     */
    private void updateControlStatus() {
        try {
            // 检查时间控制器状态
            TimeController timeController = new TimeController();
            boolean enabled = timeController.isEnabled();
            
            if (enabled) {
                controlStatusLabel.setText("已开启");
                controlStatusLabel.setStyle("-fx-text-fill: green;");
            } else {
                controlStatusLabel.setText("已关闭");
                controlStatusLabel.setStyle("-fx-text-fill: gray;");
            }
        } catch (Exception e) {
            controlStatusLabel.setText("异常: " + e.getMessage());
            controlStatusLabel.setStyle("-fx-text-fill: red;");
        }
    }

    /**
     * 更新注册状态信息
     */
    private void updateRegistrationStatus() {
        try {
            // 检查注册状态
            RegistrationManager registrationManager = new RegistrationManager();
            boolean registered = registrationManager.isRegistered();
            
            if (registered) {
                registrationLabel.setText("已注册");
                registrationLabel.setStyle("-fx-text-fill: green;");
            } else {
                registrationLabel.setText("未注册");
                registrationLabel.setStyle("-fx-text-fill: red;");
            }
        } catch (Exception e) {
            registrationLabel.setText("异常: " + e.getMessage());
            registrationLabel.setStyle("-fx-text-fill: red;");
        }
    }
}